package com.develop.data.loan.manager.change.transfer;

import com.develop.data.loan.manager.change.amount.L0001B0001LoanInstallmentInterestClear;
import com.develop.data.loan.manager.change.amount.L0001B0001LoanInstallmentInterestOverdueClear;
import com.develop.data.loan.manager.change.amount.L0001B0001LoanInstallmentPrincipalClear;
import com.develop.data.loan.manager.change.amount.L0001B0001LoanInstallmentPrincipalOverdueClear;
import com.develop.data.loan.manager.change.transfer.api.LoanInstallmentStateTransfer;
import com.develop.data.loan.model.LoanInstallment;
import lombok.extern.slf4j.Slf4j;
import org.javamoney.moneta.Money;

import javax.money.Monetary;
import javax.money.MonetaryAmount;
import java.time.LocalDateTime;

@Slf4j
public class LoanInstallmentCommonToCleared implements LoanInstallmentStateTransfer {

    @Override
    public MonetaryAmount preview(LoanInstallment installment, LocalDateTime localDate) {

        /* 金额只分逾期和非逾期的，从表的设计就可以看出，逾期和非逾期，有金额字段，非应计和核销字段非金额字段，而是起标志功能的字段 */
        /* 非应计指利息是否增加，金额还是逾期或是非逾期的，从本质上讲，非逾期的也可以停止计息，逾期的也可以停止计息 */
        /* 核销，同非应计类似，也是给分期/合约打个标记，从本质上讲，非逾期的也可以核销，逾期的也可以核销 */

        MonetaryAmount amount = Money.zero(Monetary.getCurrency("CNY"));

        amount = amount.add(
            new L0001B0001LoanInstallmentInterestClear().preview(installment, localDate)
        );

        amount = amount.add(
            new L0001B0001LoanInstallmentInterestOverdueClear().preview(installment, localDate)
        );

        amount = amount.add(
                new L0001B0001LoanInstallmentPrincipalClear().preview(installment, localDate)
        );

        amount = amount.add(
            new L0001B0001LoanInstallmentPrincipalOverdueClear().preview(installment, localDate)
        );

        return amount;

    }

    @Override
    public void execute(LoanInstallment installment, LocalDateTime localDate) {

        new L0001B0001LoanInstallmentInterestClear().execute(installment, localDate);
        new L0001B0001LoanInstallmentInterestOverdueClear().execute(installment, localDate);
        new L0001B0001LoanInstallmentPrincipalClear().execute(installment, localDate);
        new L0001B0001LoanInstallmentPrincipalOverdueClear().execute(installment, localDate);

        /* 设置结清标志 */
        installment.setCleared(true);

    }
}

